home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / AmigaSystem / Scalos / PreferencesLib / examples / C / loadsave.c < prev    next >
C/C++ Source or Header  |  2002-10-28  |  3KB  |  88 lines

  1. /*  Simple example of loading and saving preference files. */
  2.  
  3. /* Always included first */
  4. #include <exec/types.h>
  5.  
  6. /* So we can use the functions from these libraries */
  7. #include <proto/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/preferences.h>
  10.  
  11. /* For the MAKE_ID macro */
  12. #include <libraries/iffparse.h>
  13.  
  14. /* For the PrefsStruct structure */
  15. #include <scalos/preferences.h>
  16.  
  17. /* some standard stuff */
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21.  
  22. /* Pointer to the preferences.library base */
  23. struct Library  *PreferencesBase;
  24.  
  25.  
  26. int main(void)
  27. {
  28.     STRPTR  prefname = "Example";       /* Name to refer to our PrefsHandle with */
  29.     APTR    prefhandle;                 /* Handle to our preferences set */
  30.     ULONG   ID, Tag;                    /* ID and Tag are used to access a specific preferences item */
  31.     struct PrefsStruct  *prefstruct;    /* Used to find the data in the PrefsHandle */
  32.     char    temp[128];                  /* Space to retrieve the data from the PrefsHandle */
  33.     STRPTR  preffile = "example.prefs"; /* filename to load and save prefs as (does NOT need to be the same as the name of the PrefHandle) */
  34.  
  35.  
  36.     if(NULL != (PreferencesBase = OpenLibrary("preferences.library", 39)) )
  37.     {
  38.         /* First we need to allocate a PrefsHandle by name */
  39.         if(NULL != (prefhandle = AllocPrefsHandle(prefname)) )
  40.         {
  41.             printf("PrefsHandle successfully allocated\n");
  42.  
  43.             /* Try to load preferences from file */
  44.             ReadPrefsHandle(prefhandle, preffile);
  45.  
  46.             /* We need an ID and Tag value to refer to a specific preference.
  47.              * so we create that here (you could always just put them in the library calls).
  48.              * Remember that the ID must be created from 4 ASCII characters and the Tag
  49.              * cannot be 0
  50.              */
  51.             ID = MAKE_ID('M','A','I','N');
  52.             Tag = 1;
  53.  
  54.             /* Use FindPreferences to check that the item has been stored */
  55.             if(NULL != (prefstruct = FindPreferences(prefhandle, ID, Tag)) )
  56.             {
  57.                 printf("Preference data found\n");
  58.  
  59.                 /* And to show that the data was actually stored, we get it back
  60.                  * using GetPreferences
  61.                  */
  62.                 GetPreferences(prefhandle, ID, Tag, temp, 128);
  63.                 printf("Contents of preference data:\n    %s\n", temp);
  64.             }
  65.  
  66.  
  67.             /* Now we set a new value for the data */
  68.             printf("Enter new string for pref (blank string to not change value) and press return:\n    ");
  69.             gets(temp);
  70.  
  71.             if(strlen(temp) > 0)
  72.             {
  73.                 SetPreferences(prefhandle, ID, Tag, temp, strlen(temp)+1);
  74.  
  75.                 /* Save the preferences out again. Run this program again if you want to check the changes */
  76.                 WritePrefsHandle(prefhandle, preffile);
  77.             }
  78.  
  79.             /* Finally, we free the PrefsHandle */
  80.             FreePrefsHandle(prefhandle);
  81.         }
  82.  
  83.         CloseLibrary(PreferencesBase);
  84.     }
  85.     return(0);
  86. }
  87.  
  88.